<?php

namespace app\wechat\core;

use yii\base\Model;

class BaseModel extends Model
{
    public $openid;

    public function init()
    {
        parent::init();
        $this->openid = defined("WX_OPENDID") ? WX_OPENDID : '';
    }


    public function smallletter(){
		return ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
    }

    public function capitalletter(){
		return ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    }

    public function checkSticket($circuit_id,$shift_id,$s_ticket){
        $res = \Cm::$db->one("SELECT `value` FROM rule_setting WHERE type=2 AND status=1 AND circuit_id={$circuit_id}");
        if(!empty($res)){
            if($res['value'] == 0){
                return $s_ticket;
            }else{
                $ordernum = \Cm::$db->one("SELECT count(b.id) AS num FROM `order` a LEFT JOIN order_passenger b ON a.id=b.order_id WHERE a.status=1 AND a.shift_id={$shift_id} AND a.order_status!=5 AND b.status=1");
                return $ordernum['num'] > $s_ticket ? 0 : $s_ticket-$ordernum['num'];
            }
        }else{
            $res = \Cm::$db->one("SELECT `value` FROM rule_setting WHERE type=2 AND status=1 AND circuit_id=0");
            if(!empty($res)){
                if($res['value'] == 0){
                    return $s_ticket;
                }else{
                    $ordernum = \Cm::$db->one("SELECT count(b.id) AS num FROM `order` a LEFT JOIN order_passenger b ON a.id=b.order_id WHERE a.status=1 AND a.shift_id={$shift_id} AND a.order_status!=5 AND b.status=1");
                    return $ordernum['num'] > $s_ticket ? 0 : $s_ticket-$ordernum['num'];
                }
            }else{
                return $s_ticket;
            }
        }
    }
}